﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SiteMapEditor.Controls
{
    public partial class GroupControl : UserControl, ISiteMapSavable
    {
        private Dictionary<string, string> collec;

        private string initialUrl = "";
        private string initialId = "";
        private bool initialIsProfile = false;
        private string initialTitle = "";
        private string initialDescription = "";
        
        #region Delegates

        public delegate void SaveEventHandler(object sender, SaveEventArgs e);

        #endregion

        #region Event Handlers

        public event SaveEventHandler Saved;

        #endregion

        public GroupControl()
        {
            InitializeComponent();

            this.collec = new Dictionary<string, string>();
           
            ToolTip tip = new ToolTip();
            tip.ToolTipTitle = "Information";
            tip.SetToolTip(this.txtGroupId, "A unique identifier for this Group element.");
            tip.SetToolTip(this.txtGroupUrl, "Specifies the URL to render for the Outlook folder that represents the Group in Microsoft Dynamics CRM for Outlook.");
            tip.SetToolTip(this.chkGroupIsProfile, "Controls whether this Group represents a user selectable Profile for the Workplace. This only applies for Groups within the Workplace Area.");
            tip.SetToolTip(this.txtGroupTitle, "Deprecated. Use the <Titles> (SiteMap) and <Title> (SiteMap) elements.");
            tip.SetToolTip(this.txtGroupDescription, "Deprecated. Use the <Description> (SiteMap) element.");
        }

        public GroupControl(Dictionary<string, string> collection):this()
        {
            this.collec = collection;

            this.FillControls();
        }

        private void FillControls()
        {
            this.txtGroupUrl.Text = collec.ContainsKey("Url") ? collec["Url"] : "";
            this.txtGroupId.Text = collec.ContainsKey("Id") ? collec["Id"] : "";
            this.chkGroupIsProfile.Checked = collec.ContainsKey("IsProfile") ? collec["IsProfile"].ToLower() == "true" : false;
            this.txtGroupResourceId.Text = collec.ContainsKey("ResourceId") ? collec["ResourceId"] : "";
            this.txtGroupDescriptionResourceId.Text = collec.ContainsKey("DescriptionResourceId") ? collec["DescriptionResourceId"] : "";
            this.txtGroupTitle.Text = collec.ContainsKey("Title") ? collec["Title"] : "";
            this.txtGroupDescription.Text = collec.ContainsKey("Description") ? collec["Description"] : "";

            this.initialUrl = this.txtGroupUrl.Text;
            this.initialId = this.txtGroupId.Text;
            this.initialIsProfile = this.chkGroupIsProfile.Checked;
            this.initialDescription = this.txtGroupDescription.Text;
            this.initialTitle = this.txtGroupTitle.Text;
        }

        private void SiteMapControl_Leave(object sender, EventArgs e)
        {
            if (this.initialUrl != this.txtGroupUrl.Text ||
                this.initialId != this.txtGroupId.Text ||
                this.initialDescription != this.txtGroupDescription.Text ||
                this.initialTitle != this.txtGroupTitle.Text ||
                this.initialIsProfile != this.chkGroupIsProfile.Checked)
            {
                if (MessageBox.Show("You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.Save();
                }
            }
        }

        public void Save()
        {
            if (this.txtGroupId.Text.Length == 0)
            {
                MessageBox.Show(this, "Id is required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Dictionary<string, string> collection = new Dictionary<string, string>();
                if (this.txtGroupUrl.Text.Length > 0)
                    collection.Add("Url", this.txtGroupUrl.Text);
                if (this.txtGroupId.Text.Length > 0)
                    collection.Add("Id", this.txtGroupId.Text);
                if (this.txtGroupResourceId.Text.Length > 0)
                    collection.Add("ResourceId", this.txtGroupResourceId.Text);
                if (this.txtGroupDescriptionResourceId.Text.Length > 0)
                    collection.Add("DescriptionResourceId", this.txtGroupDescriptionResourceId.Text);
                if (this.txtGroupDescription.Text.Length > 0)
                    collection.Add("Description", this.txtGroupDescription.Text);
                if (this.txtGroupTitle.Text.Length > 0)
                    collection.Add("Title", this.txtGroupTitle.Text);
                
                collection.Add("IsProfile", this.chkGroupIsProfile.Checked.ToString().ToLower());

                this.initialUrl = this.txtGroupUrl.Text;
                this.initialId = this.txtGroupId.Text;
                this.initialIsProfile = this.chkGroupIsProfile.Checked;
                this.initialDescription = this.txtGroupDescription.Text;
                this.initialTitle = this.txtGroupTitle.Text;

                this.SendSaveMessage(collection);
            }
        }

        #region Send Events

        /// <summary>
        /// Sends a connection success message 
        /// </summary>
        /// <param name="service">IOrganizationService generated</param>
        /// <param name="parameters">Lsit of parameter</param>
        private void SendSaveMessage(Dictionary<string, string> collection)
        {
            SaveEventArgs sea = new SaveEventArgs() { AttributeCollection = collection };

            if (this.Saved != null)
            {
                this.Saved(this, sea);
            }
        }

        #endregion

    }
}
